<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Document</title>
    </head>
    <body>
        <div>Document</div>
        <script type="module">
            let arr1 = [1, 2, 2, 1];
            let arr2 = [2, 2];

            let arr3 = [4, 9, 5];
            let arr4 = [9, 4, 9, 5, 8, 4];

            function fun(a1, a2) {
                console.log('----- ');
                console.log(a1, a2);
                if (!Array.isArray(a1) || !Array.isArray(a2)) {
                    return [];
                }
                if (a1.length === 0 || a2.length === 0) {
                    return [];
                }

                let list = [];
                let jiaojis = [];
                for (let i = 0; i < a1.length; i++) {
                    list = [];
                    for (let j = 0; j < a2.length; j++) {
                        if (a1[i + list.length] === a2[j]) {
                            list.push(a2[j]);
                        } else {
                            if (list.length > 0 && j != 0) {
                                break;
                            }
                        }
                    }
                    // console.log("==>", list);
                    if (list.length != 0) jiaojis.push(list);
                }
                console.log('==>', jiaojis);
                return jiaojis;
            }

            /**
             * @param {number[]} nums1
             * @param {number[]} nums2
             * @return {number[]}
             */
            var intersect = function (nums1, nums2) {
                let data = {},
                    result = [];
                nums1.forEach((i) => {
                    if (data[i]) {
                        data[i]++;
                    } else {
                        data[i] = 1;
                    }
                });
                console.log(JSON.parse(JSON.stringify(data)));

                nums2.forEach((j) => {
                    console.log('---->');
                    console.log(result);
                    console.log(j, data[j]);
                    if (data[j]) {
                        console.log('--');
                        data[j]--;
                        result.push(j);
                    }
                });

                return result;
            };

            console.log(fun(arr1, arr2)); // [2,2]
            console.log(fun(arr2, arr1)); // [2,2]
            console.log(fun(arr3, arr4)); // [4,9]
            console.log(fun(arr4, arr3)); // [4,9]
        </script>
    </body>
</html>
